package com.xrtn.controller;

import com.xrtn.bean.*;
import com.xrtn.service.ICommonService;
import com.xrtn.service.IRecordService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.HttpUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.service.IRoleService;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by tu_pei on 2018/2/28.
 */
@Api("基础数据模块")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/init")
public class InitController {

    @Autowired
    private ICommonService commonService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRecordService recordService;


    /**
     * 进入商品管理页面
     * @return
     */
    @RequestMapping(value = "/gotoGoodsSetting",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoGoodsSetting(ModelAndView modelAndView){
        modelAndView.setViewName("goodsSetting");
        return modelAndView;
    }

    /**
     * 进入推拿管理页面
     * @return
     */
    @RequestMapping(value = "/gotoServiceSetting",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoServiceSetting(ModelAndView modelAndView){
        modelAndView.setViewName("serviceSetting");
        return modelAndView;
    }
    /**
     * 进入新增商品管理页面
     * @return
     */
    @RequestMapping(value = "/gotoGoodsSettingAdd",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoAddGoodsSetting(ModelAndView modelAndView){
        modelAndView.setViewName("goodsSettingAdd");
        return modelAndView;
    }
    /**
     * 进入新增商品管理页面
     * @return
     */
    @RequestMapping(value = "/gotoGoodsSettingCost",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoGoodsSettingCost(ModelAndView modelAndView){
        modelAndView.setViewName("goodsSettingCost");
        return modelAndView;
    }
    /**
     * 进入新增商品管理页面(已废)
     * @return
     */
    @RequestMapping(value = "/gotoGoodsSettingCostDetail",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoGoodsSettingCostDetail(ModelAndView modelAndView){
        modelAndView.setViewName("goodsSettingCostDetail");
        return modelAndView;
    }
    /**
     * 进入新增推拿管理页面
     * @return
     */
    @RequestMapping(value = "/gotoServiceSettingAdd",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoAddServiceSetting(ModelAndView modelAndView){
        modelAndView.setViewName("serviceSettingAdd");
        return modelAndView;
    }

    /**
     * @Title: selectSysList
     * @Description: 获取系统初始数据
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "获取系统初始数据",notes="获取系统初始数据")
    @RequestMapping(value = "/selectSysList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectSysList(){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        Map<String,Object> retMap = new HashMap<>();
        retMap.put("roleList", roleService.selectSysRoleList());
        retMap.put("gradeList", commonService.selectDoctorGradeList());
        retMap.put("fromList", commonService.selectInitFromAll());
        Map<String,Object> params = new HashMap<>();

        params.put("typeId",2);
        retMap.put("yearLabelList", commonService.selectInitLabelAll(params));
        params.put("typeId",3);
        retMap.put("titleLabelList", commonService.selectInitLabelAll(params));
        params.put("typeId",4);
        retMap.put("levelLabelList", commonService.selectInitLabelAll(params));


        retMap.put("specialList", recordService.selectRecordAll());
        List<InitDiseaseType> diseaseList = commonService.selectDiseaseTypeList();

        List<Map<String, Object>> retList = new ArrayList<>();
        for (InitDiseaseType disease : diseaseList) {
            Map<String, Object> diseMap = new HashMap<>();
            diseMap.put("typeId", disease.getTypeId());
            diseMap.put("isMain", disease.getIsMain());
            diseMap.put("typeName", disease.getTypeName());
            diseMap.put("typeStatus", disease.getTypeStatus());
            diseMap.put("childList", commonService.selectDiseaseInfoList(disease.getTypeId()));
            retList.add(diseMap);
        }

        retMap.put("diseaseList" ,retList );

        List<Map<String, Object>> pointList = commonService.selectPointGroupList();

        retMap.put("pointList", pointList);

        data.setResults(retMap);
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectDiseaseTypeList
     * @Description: 查询症状类型列表
     */
    @ApiOperation(value = "查询症状类型列表", notes = "查询症状类型列表")
    @RequestMapping(value = "/selectDiseaseTypeList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectDiseaseTypeList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        List<InitDiseaseType> diseaseList = commonService.selectDiseaseTypeList();

        List<Map<String, Object>> retList = new ArrayList<>();
        for (InitDiseaseType disease : diseaseList) {
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("typeId", disease.getTypeId());
            retMap.put("isMain", disease.getIsMain());
            retMap.put("typeName", disease.getTypeName());
            retMap.put("typeStatus", disease.getTypeStatus());
            retMap.put("childList", commonService.selectDiseaseInfoList(disease.getTypeId()));
            retList.add(retMap);
        }

        data.setResults(retList);
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectDiseaseInfoList
     * @Description: 根据主症类型查询病症列表
     */
    @ApiOperation(value = "根据主症类型查询病症列表", notes = "根据主症类型查询病症列表")
    @RequestMapping(value = "/selectDiseaseInfoList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectDiseaseInfoList(Integer typeId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectDiseaseInfoList(typeId));
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectPointGroupList
     * @Description: 查询穴位分组信息
     */
    @ApiOperation(value = "查询穴位分组信息", notes = "查询穴位分组信息")
    @RequestMapping(value = "/selectPointGroupList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectPointGroupList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectPointGroupList());
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitServiceList
     * @Description: 查询推拿信息列表
     */
    @ApiOperation(value = "查询推拿信息列表", notes = "查询推拿信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "state", dataType = "int", required = true, value = "0:全部，1上架", defaultValue = "1"),
            @ApiImplicitParam(paramType = "string", name = "serviceName", dataType = "int", required = false, value = "搜索关键字", defaultValue = "小儿推拿"),
    })
    @RequestMapping(value = "/selectInitServiceList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectInitServiceList(Integer userId, Integer state, String serviceName) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitServiceList(userId, state, serviceName));
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitGoodsList
     * @Description: 查询药品列表
     */
    @ApiOperation(value = "查询药品列表", notes = "查询药品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "state", dataType = "int", required = true, value = "0:全部，1上架", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectInitGoodsList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectInitGoodsList(Integer state,String goodsName) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitGoodsList(state, goodsName));
        // 返回数据
        return data;
    }


    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitConclusionList
     * @Description: 查询诊断结论列表
     */
    @ApiOperation(value = "查询诊断结论列表", notes = "查询诊断结论列表")
    @RequestMapping(value = "/selectInitConclusionList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectInitConclusionList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitConclusionList());
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: saveServiceInfo
     * @Description: 新增推拿服务
     */
    @ApiOperation(value = "新增推拿服务", notes = "新增推拿服务")
    @RequestMapping(value = "/saveServiceInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "serviceName", dataType = "string", required = true, value = "服务名称", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceImg", dataType = "string", required = true, value = "服务图片", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceCost", dataType = "double", required = true, value = "80.00", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceIncome", dataType = "double", required = true, value = "10.00", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData saveServiceInfo(HttpServletRequest request,InitServiceInfo serviceInfo) {

        SysUserInfo userInfo = (SysUserInfo) request.getSession().getAttribute(HttpUtils.CURRENT_USER_4_POJO);

        // 创建响应Model
        ResponseData data = new ResponseData();

        /**
         *   jyf  先注释掉角色判断
         */
//        if(userInfo.getUserRole() == 2 && userInfo.getSubclinicId() == 3){
            // 调用Service
            commonService.saveServiceInfo(serviceInfo);
            data.setResults(serviceInfo.getServiceId());
            return data;
//        }else{
//            data.setCodeEnum(CodeEnum.CODE_70001);
//            return data;
//        }
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectServiceInfo
     * @Description: 查询推拿服务
     */
    @ApiOperation(value = "查询推拿服务", notes = "查询推拿服务")
    @RequestMapping(value = "/selectServiceInfo", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "serviceId", dataType = "int", required = true, value = "服务Id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectServiceInfo(Integer serviceId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        data.setResults(commonService.selectServiceInfo(serviceId));
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: updateServiceInfo
     * @Description: 修改商品服务服务
     */
    @ApiOperation(value = "修改商品服务服务", notes = "修改商品服务服务")
    @RequestMapping(value = "/updateGoodsInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodsId", dataType = "int", required = true, value = "商品Id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsName", dataType = "string", required = false, value = "商品名称", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsImg", dataType = "string", required = false, value = "商品图片", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsState", dataType = "int", required = false, value = "状态：0下架，1上架", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsCost", dataType = "double", required = false, value = "80.00", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsIncomeRatio", dataType = "double", required = false, value = "0.1", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData updateGoodsInfo(InitGoodsInfo goodsInfo) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        data.setResults(commonService.updateGoodsInfo(goodsInfo));
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: updateServiceInfo
     * @Description: 修改推拿服务
     */
    @ApiOperation(value = "修改推拿服务", notes = "修改推拿服务")
    @RequestMapping(value = "/updateServiceInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "serviceId", dataType = "int", required = true, value = "服务Id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceName", dataType = "string", required = false, value = "服务名称", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceImg", dataType = "string", required = false, value = "服务图片", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceStatus", dataType = "int", required = false, value = "状态：0下架，1上架", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceCost", dataType = "double", required = false, value = "80.00", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceIncome", dataType = "double", required = false, value = "10.00", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData updateServiceInfo(InitServiceInfo serviceInfo) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        data.setResults(commonService.updateServiceInfo(serviceInfo));
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: saveGoodsInfo
     * @Description: 新增药品服务
     */
    @ApiOperation(value = "新增药品服务", notes = "新增药品服务")
    @RequestMapping(value = "/saveGoodsInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodsName", dataType = "string", required = true, value = "商品名称", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsImg", dataType = "string", required = true, value = "商品图片", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsCost", dataType = "double", required = true, value = "80.00", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "goodsIncome", dataType = "double", required = true, value = "10.00", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData saveGoodsInfo(InitGoodsInfo goodsInfo) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        commonService.saveGoodsInfo(goodsInfo);
        data.setResults(goodsInfo.getGoodsId());
        // 返回数据
        return data;
    }

    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectGoodsInfo
     * @Description: 查询药品服务
     */
    @ApiOperation(value = "查询药品", notes = "查询药品")
    @RequestMapping(value = "/selectGoodsInfo", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodsId", dataType = "int", required = true, value = "药品id", defaultValue = "1"),
    })
    @ResponseBody
    public ResponseData selectGoodsInfo(Integer goodsId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        data.setResults(commonService.selectGoodsInfo(goodsId));
        // 返回数据
        return data;
    }

    /**
     * 查询商品消耗列表下（分类）
     *
     * @param subclinicId
     * @param ifMonth
     * @param orderTime
     * @return
     */
    @ApiOperation(value = "查询商品消耗列表", notes = "查询商品消耗列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "门店id(全部传0,)", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "ifMonth", dataType = "int", required = true, value = "月/日汇总（0，日 1，月）", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "orderTime", dataType = "String", required = true, value = "汇总时间", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectOrderList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectOrderList(int subclinicId, int ifMonth, String orderTime) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{subclinicId, ifMonth, orderTime});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            List<Map<String,Object>> goodsDetails = commonService.selectGoodsList(subclinicId, ifMonth, orderTime);
            List<Map<String,Object>> goodsLists = commonService.selectGoodsListDetail(subclinicId, ifMonth, orderTime);

            Map<String, Object> map = new HashMap<>();
            Integer orderNum = 0;
            if( goodsLists.size() > 0 ){
                for (Map<String,Object> goodsMap : goodsDetails){
                    orderNum += Integer.parseInt(goodsMap.get("goodsNumber").toString());
                }
            }
            map.put("total", orderNum);
            map.put("totalType", goodsLists.size());
            map.put("goodList", goodsDetails);
            data.setResults(map);
            // 返回数据
            return data;
        }

    }

    /**
     * 查询商品消耗列表(详情)
     *
     * @param subclinicId
     * @param ifMonth
     * @param orderTime
     * @return
     */
    @ApiOperation(value = "消耗列表详情", notes = "消耗列表详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "门店id(全部传0,)", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "ifMonth", dataType = "int", required = true, value = "月/日汇总（0，日 1，月）", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "orderTime", dataType = "String", required = true, value = "汇总时间(格式 xxxx-xx-xx)", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectOrderListDetail", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectOrderListDetail(int subclinicId, int ifMonth, String orderTime) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{subclinicId, ifMonth, orderTime});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            List<Map<String,Object>> goodsDetails = commonService.selectGoodsListDetail(subclinicId, ifMonth, orderTime);

            data.setResults(goodsDetails);
            // 返回数据
            return data;
        }
    }
}
